package com.dev.utils;

import com.dev.receiver.MyRadioStateObserver;
import ohos.aafwk.ability.AbilitySlice;
import ohos.eventhandler.EventRunner;
import ohos.telephony.RadioInfoManager;
import ohos.telephony.RadioStateObserver;

/**
 * 网络状态监听工具类
 */
public class NetWorkUtils {

    private static RadioInfoManager radioInfoManager;

    /**
     * 执行回调的runner；创建MyRadioStateObserver的对象；添加回调。
     */
    public static MyRadioStateObserver initNetWork(AbilitySlice slice) {
        // 获取RadioInfoManager对象
        radioInfoManager = RadioInfoManager.getInstance(slice);
        EventRunner runner = EventRunner.create();
        //slotId为要观察的卡槽ID（0或1）
        MyRadioStateObserver observer = new MyRadioStateObserver(0,runner,slice);
        return observer;
    }

    /**
     * 初始化网络监听
     */
    public static void subscribeNetWork(MyRadioStateObserver observer) {
        radioInfoManager.addObserver(observer, RadioStateObserver.OBSERVE_MASK_NETWORK_STATE
                | RadioStateObserver.OBSERVE_MASK_SIGNAL_INFO);
    }

    /**
     * 注销网络监听
     */
    public static void unsubscribeNetWork(MyRadioStateObserver observer) {
        radioInfoManager.removeObserver(observer);
    }
}
